package user

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"project-im/config"
	"project-im/internal/api/request"
	"project-im/internal/model/dao"
	"project-im/internal/model/service"
)

func Register(c *gin.Context) {
	var json request.Registers
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	userName := json.UserName
	nickName := json.NickName
	password := json.Password
	//passwords := json.Passwords
	if service.IsRegister(userName) == true {
		c.JSON(http.StatusBadRequest, gin.H{"error": "该用户已被注册"})
		return
	}
	//if password != passwords {
	//	c.JSON(http.StatusBadRequest, gin.H{"error": "两次输入的密码不一致"})
	//	return
	//}
	user := dao.ImUser{
		UserName: userName,
		NickName: nickName,
		Password: password,
	}
	config.DB.Create(&user)
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "注册成功",
		"data":    nil,
	})
}
